package com.yogee.topseller.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.yogee.topseller.R;
import com.yogee.topseller.activity.ProductDetailsActivity;
import com.yogee.topseller.bean.InterlligentFilterBean;
import com.yogee.topseller.bean.MySelfInfo;
import com.yogee.topseller.utils.AppUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/6/26.
 */

public class IntelligentScreeningListRvAdapter extends RecyclerView.Adapter<IntelligentScreeningListRvAdapter.ItemHolder> {

    private Context context;
    private List<InterlligentFilterBean.ProductListBean> beans = new ArrayList<>();

    public IntelligentScreeningListRvAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<InterlligentFilterBean.ProductListBean> beans) {
        this.beans = beans;
        notifyDataSetChanged();
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_is_list, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(ItemHolder holder, int position) {
        final InterlligentFilterBean.ProductListBean bean = beans.get(position);
        holder.nameTv.setText(bean.getName());
        holder.qtMoneyTv.setText(bean.getStartAmount());
        try{
            holder.syTv.setText(bean.getExpectReturn().substring(0,bean.getExpectReturn().length()-1));
        }catch(Exception e){
            holder.syTv.setText("8.0");
        }

        if (!bean.getInvestmentTerm().endsWith("个月")) {
            holder.monthTv.setText(bean.getInvestmentTerm() + "个月");
        }else{
            holder.monthTv.setText(bean.getInvestmentTerm());
        }
        if(AppUtil.isExamine(context)){
            holder.yongBiTv.setText(bean.getCommission());
            holder.yongBiTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
            // 判断是否为会员
            if(MySelfInfo.getInstance().getIsPay().equals("0")){
                holder.yongProTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                if (null != bean.getPercent() && !"".equals(bean.getPercent())) {
                    //33.33
//            holder.yongPb.setProgress(Integer.valueOf(bean.getPercent()));
                    String[] splitPres = new String[2];
                    splitPres = bean.getPercent().split("\\.");
                    if (splitPres.length>1){
                        holder.yongPb.setProgress(Integer.parseInt(splitPres[0]));
                    }else{
                        holder.yongPb.setProgress(Integer.parseInt(bean.getPercent()));
                    }
                } else if (null != bean.getPercent() && bean.getPercent().endsWith("%")) {
                    String[] splitPres = new String[2];
                    splitPres = bean.getPercent().substring(0, bean.getPercent().length() - 1).split("\\.");
                    if (splitPres.length>1){
                        holder.yongPb.setProgress(Integer.parseInt(splitPres[0]));
                    }else{
                        holder.yongPb.setProgress(Integer.parseInt(bean.getPercent()));
                    }
//            holder.yongPb.setProgress(Integer.valueOf(bean.getPercent().substring(0, bean.getPercent().length() - 1)));
                }
                if (null != bean.getPercent() && !bean.getPercent().endsWith("%")) {
                    holder.yongProTv.setText(bean.getPercent() + "%");
                }
            }else{
                holder.yongProTv.setText("会员可见");
                holder.yongProTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                holder.yongPb.setProgress(0);
            }
        } else{
            holder.yongBiTv.setText("请登录");
            holder.yongBiTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
            holder.yongPb.setProgress(0);
            holder.yongProTv.setText("请登录");
            holder.yongProTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
        }



//        String labels = "";
//        List<String> list = Arrays.asList(bean.getLabels().split(","));
//        for (int i = 0; i < list.size(); i++) {
//            labels += " " + list.get(i);
//        }

        holder.detailsTv.setText(bean.getProType());
        holder.itemListLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //7707b863d305425e84f7d9004227101b   bean.getId()  (下架测试)4e149ad8aad74b1aa744f54c574b857c
                context.startActivity(new Intent(context, ProductDetailsActivity.class).putExtra("id",bean.getId()).putExtra("isFast", "0"));
            }
        });

        // 预热和图片
        if(bean.getImgstuats().equals("1")){
            holder.img_tu.setVisibility(View.VISIBLE);
        }else{
            holder.img_tu.setVisibility(View.GONE);
        }

        if(bean.getStatus().equals("预热中"))
            holder.img_yu.setVisibility(View.VISIBLE);
        else{
            holder.img_yu.setVisibility(View.GONE);
        }
        if(bean.getStatus().equals("售罄")){
            holder.img_sell_out.setVisibility(View.VISIBLE);
        }else
            holder.img_sell_out.setVisibility(View.GONE);


    }

    @Override
    public int getItemCount() {
        return beans == null ? 0 : beans.size();
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_list_ll)
        LinearLayout itemListLl;
        @BindView(R.id.item_list_name_tv)
        TextView nameTv;
        @BindView(R.id.item_list_mj_tv)
        TextView miaoTv;
        @BindView(R.id.item_list_details_tv)
        TextView detailsTv;
        @BindView(R.id.item_list_qt_money_tv)
        TextView qtMoneyTv;
        @BindView(R.id.item_list_sy_tv)
        TextView syTv;
        @BindView(R.id.item_list_month_tv)
        TextView monthTv;
        @BindView(R.id.item_list_yong_bi_tv)
        TextView yongBiTv;
        @BindView(R.id.item_list_yong_pro_tv)
        TextView yongProTv;
        @BindView(R.id.item_list_yong_pb)
        ProgressBar yongPb;
        @BindView(R.id.img_yu)
        ImageView img_yu;
        @BindView(R.id.img_tu)
        ImageView img_tu;
        @BindView(R.id.img_sell_out)
        ImageView img_sell_out;

        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}